/**
 * 
 */
/**
 * 
 */


issues = {};

var issue = null;
var issue_id = null;
$(document).on("pagebeforeshow", "#issueShow", function(event) {
	WL.Logger.debug("IssueShow::on::pagebeforeshow");
	busyIndicator.hide();
	issue_id = sessionStorage.selectedIssueId;
	if (issue_id) {
		issues.showIssue(issue_id);
	}
});


issues.showIssue = function(issue_id) {
	busyIndicator = new WL.BusyIndicator("content", {
		text : "Loading..."
	});
	busyIndicator.show();
	WL.Logger.debug("Issues::invokeProcedure::GetIssue");
	var invocationData = {
		adapter : "Redmine",
		procedure : "getIssue",
		parameters : [ issue_id ]
	};
	// busyIndicator.show();
	WL.Client
			.invokeProcedure(
					invocationData,
					{
						onSuccess : function(response) {

							if (response.invocationResult.statusCode == 200) {
								WL.Logger
										.debug("IssueShow::invokeProcedure::Successful");
								issue = response.invocationResult.issue;
								if (issue!=null){
									issues.renderShowIssue(issue);
								}
							} else
								alert("Invoke getProjects Procedure Failure.StatusCode!=200. Response :: "
										+ JSON.stringify(error));

						},
						onFailure : function(response) {
							if (response.invocationResult.authStatus == "required") {
								customAuthenticatorRealmChallengeHandler
										.handleChallenge(response);
							} else {
								WL.Logger
										.debug("ProjectIssues::invokeProcedure::Failure. Error = "
												+ JSON.stringify(error));
								alert("Invoke getProjects Procedure Failure. Response :: "
										+ JSON.stringify(error));
							}
						}
					});
	return null;
};

issues.renderShowIssue = function(issue) {
	WL.Logger.debug("Issues::RenderShowIssues");
	$('#issueTitle').text("#"+ issue.id+ "  "+ issue.subject);
	$('#tableTitle').text("#"+ issue.id+ "  "+ issue.subject);
	$('#issueUpdatedOn').html(issue.updated_on);
	$('#issueSubject').html(issue.subject);
	$('#issueDesc').html((typeof issue.description === 'undefined')?"No Description":issue.description);
	$('#issueDoneProgress').attr({"max":100,"value": ((typeof issue.done_ratio === 'undefined')?"0%":(issue.done_ratio+"%"))});
	
	$('#issueStatus').html(issue.status.name);
	$('#issuePriority').html(issue.priority.name);
	$('#issueAssignedTo').html((typeof issue.assigned_to === 'undefined')?"Not Assigned":issue.assigned_to.name);
	$('#issueSpentHours').html("Spent Hours: " + issue.spent_hours);	
	$('#issueStartDate').html((typeof issue.start_date === 'undefined')?"Start Date:_":issue.start_date);
	$('#issueDueDate').html((typeof issue.due_date === 'undefined')?"Due Date:_":issue.due_date);
	$('#issueFixedVersion').html((typeof issue.fixed_version === 'undefined')?"No Target Version":issue.fixed_version);
	
	busyIndicator.hide();

};

$(document).on("pageshow", "#issueShow", function() {
/*	$('#issueShowBackButton').on('click', function (e) {
	/*	  e.preventDefault();
		  var page = document.referrer;
	
		$.mobile.changePage("ProjectIssues.html", {
		    transition: 'flip',changeHash : false,
		    reverse: true
		  });
		});
	*/
	$('.back-button').bind('click', function() {
		pageHistory.back();
	});
	$('.refresh-button').bind('click', function() { 
		pageHistory.navigateToPage("issueShow",{transition: "slide", allowSamePageTransition : true });
	});
});


